package com.safetyprotection.SafetyProtectionManageD;

import android.content.Context;

import com.safetyprotection.AppFlyer.AppsFlyerCallBase;
import com.safetyprotection.SafetyProtectionManageD.Bean.SafetyProtectionBean;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URLEncoder;
import java.util.HashMap;

/**
 * @类描述: 主配置类
 * @项目名称: SafetyProtection
 * @包名称: com.safetyprotection
 * 使用示例：
 * （1）建议在Application 中进行初始话操作 功能列表 ①动态设置AppFlyer 的 AF_DEV_KEY
 * ②初始化归因Appsflyer ③ 是否开启地理围栏 （注：如开启地理围栏Activity需基础BaseActivity类，以获取定位权限）
 * <p>
 * SafetyProtectionManage.getInstance()
 * .setAF_DEV_KEY("65KGjam4H3ZB5GVdDEmpp6") // 动态设置AppFlyer 的 AF_DEV_KEY
 * .initAppsflyer(this) // 初始化归因Appsflyer
 * .IslocationTaskEnabled(true); // 是否开启地理围栏 如开启地理围栏Activity需基础BaseActivity类，以获取定位权限
 * <p>
 * （2）获取地理围栏状态
 * <p>
 * boolean islocationTask = SafetyProtectionManage.getInstance().isIslocationTask();
 * <p>
 * (3) 发送归因事件
 * <p>
 * SafetyProtectionManage.getInstance().Logevent("key",new HashMap<>());
 * <p>
 * (4) 获取安防数据
 * <p>
 * SafetyProtectionManage.getInstance().getSafetyProtectionData();
 * <p>
 * (4) javaBean转URL拼参数
 * <p>
 * SafetyProtectionManage.getInstance().buildUrlFromBean(bean);
 * <p>
 * ==================================================
 * @创建人: 雾都雄狮
 * @创建时间: 2024/1/3 14:25
 * @version: V1.0
 * ==================================================
 */
public class SafetyProtectionManage {
    private static SafetyProtectionManageDelegate mSafetyProtectionManageDelegate;

    /**
     * 创建一个 SafetyProtectionManageDelegate 对象
     *
     * @return SafetyProtectionManageDelegate
     * ==================================================
     * @author: 张强 (VX:15354069488)
     * @version: V3.0  2022/9/19 16:07
     * ==================================================
     */
    public static SafetyProtectionManageDelegate getSafetyProtectionManageDelegate() {
        if (mSafetyProtectionManageDelegate == null) {
            mSafetyProtectionManageDelegate = SafetyProtectionManageDelegate.create();
        }
        return mSafetyProtectionManageDelegate;
    }

    private static SafetyProtectionManage instance;

    public static SafetyProtectionManage getInstance() {
        if (instance == null) {
            synchronized (SafetyProtectionManage.class) {
                if (instance == null) {
                    instance = new SafetyProtectionManage();
                }
            }
        }
        return instance;
    }

    /**
     * 1. 动态设置AppFlyer 的 AF_DEV_KEY
     * ==================================================
     *
     * @author: 雾都雄狮
     * @version: V1.0  2024/1/2
     * ==================================================
     **/
    public SafetyProtectionManage setAF_DEV_KEY(String AF_DEV_KEY) {
        getSafetyProtectionManageDelegate().setAF_DEV_KEY(AF_DEV_KEY);
        return instance;
    }

    /**
     * 2. 初始化归因Appsflyer
     *
     * <p>
     * ==================================================
     *
     * @author: 雾都雄狮
     * @version: V1.0  2024/1/2
     * ==================================================
     **/
    public SafetyProtectionManage initAppsflyer(Context context, AppsFlyerCallBase appsFlyerCallBase) {
        getSafetyProtectionManageDelegate().initAppsflyer(context, appsFlyerCallBase);
        return instance;
    }

    /**
     * 3.是否开启地理围栏
     * <p>
     * ==================================================
     *
     * @author: 雾都雄狮
     * @version: V1.0  2024/1/3
     * ==================================================
     **/
    public SafetyProtectionManage IslocationTaskEnabled(boolean IslocationTask) {
        getSafetyProtectionManageDelegate().IslocationTaskEnabled(IslocationTask);
        return instance;
    }

    /**
     * 4.获取地理围栏状态
     * <p>
     * ==================================================
     *
     * @author: 雾都雄狮
     * @version: V1.0  2024/1/3
     * ==================================================
     **/
    public boolean isIslocationTask() {
        return getSafetyProtectionManageDelegate().isIslocationTask();
    }

    /**
     * 5.发送归因事件
     * <p>
     * ==================================================
     *
     * @author: 雾都雄狮
     * @version: V1.0  2024/1/2
     * ==================================================
     **/
    public void Logevent(String eventName, HashMap<String, Object> eventValue) {
        getSafetyProtectionManageDelegate().Logevent(eventName, eventValue);
    }

    /**
     * 6.获取安防数据
     * ==================================================
     *
     * @author: 雾都雄狮
     * @version: V1.0  2024/1/2
     * ==================================================
     **/
    public SafetyProtectionBean getSafetyProtectionData() {
        return getSafetyProtectionManageDelegate().getSafetyProtectionData();
    }

    /**
     * 7.javaBean转URL拼参数
     * ==================================================
     *
     * @author: 雾都雄狮
     * @version: V1.0  2024/1/2
     * ==================================================
     **/
    public static <T> String buildUrlFromBean(T bean) throws IllegalAccessException {
        StringBuilder sb = new StringBuilder();
        sb.append("?");
        Class<?> clazz = bean.getClass();
        Field[] fields = clazz.getDeclaredFields();

        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];

            if (!field.isSynthetic()) { // 跳过合成字段（如编译器自动生成的）
                if (Modifier.isStatic(field.getModifiers())) {
                    continue; // 忽略 static 字段
                }
                field.setAccessible(true);

                Object value = field.get(bean);
                String encodedValue = null;
                String encodedKey = null;
                try {
                    encodedValue = URLEncoder.encode((value != null ? value : "").toString(), "UTF-8");
                    encodedKey = URLEncoder.encode(field.getName(), "UTF-8");

                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
                sb.append(encodedKey).append("=").append(encodedValue).append('&');
            }
        }

        return sb.deleteCharAt(sb.length() - 1).toString(); // 删除最后多余的'&'符号
    }
}
